package com.demo.cms.action.user;

import org.apache.commons.lang3.StringUtils;

import com.demo.Constants;
import com.demo.cms.model.Channel;
import com.demo.cms.service.ChannelService;
import com.demo.core.model.Template;
import com.demo.webapp.action.TemplateProvider;

/**
 * 统计Action
 * 前端页面访问统计的Acttion
 * <br>由于页面的一些缓存设置,统计信息需要js读取
 * @author ryuu.kk
 *
 */
public class RecordAction extends ClientBaseAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 5457908809351588642L;

	/**
	 * 频道模型
	 */
	private Channel channel;
	
	/**
	 * 模板模型
	 */
	private Template template;
	
	@Override
	public String execute() {
		
		// 请求的前期处理
		super.prepare();
		
		Long id = getId();
		Long siteId = getSite().getId();
		String channelPath = getChannelPath();
		ChannelService channelService = getChannelService();
		if (StringUtils.isNotEmpty(channelPath)) {
			channel = channelService.find(channelPath);
		} else {
			if (id == null) {
				channel = channelService.findDefault(siteId);
			} else {
				channel = channelService.find(id);
			}
		}
		if (channel == null) {
			// 找不到栏目模型,提示错误
			// 找到错误提示模板,并生成错误提示.
			// 错误提示模板将与其他模板一样,可编辑.
			// do some thing;
			addActionError(getText("未找到指定的频道."));
			return INPUT;
		}
		// 取得渲染模板
		template = findTemplate(channel.getTemplateId());
		return SUCCESS;
	}
	
	// ~~ setter here ============================
	
	public String getActionType() {
		return Constants.ACTION_TYPE_CHANNEL;
	}
	
	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	@Override
	public Template getTemplateModel() {
		return this.template;
	}
}
